#coding=utf-8
import os,time,ConfigParser,codecs
import urllib2

class Brusher:
    '''A Simple Brusher'''
    def __init__(self, conf='Config.ini'):
        self.URL='http://pncdzx2011.blog.163.com/'
        self.INTERVAL=0
        self.MAX=200
        self.TIMEOUT=300
        self.CONF=conf
        self.readConfig()

    def readConfig(self):
        '''read config from a file'''
        cf=ConfigParser.ConfigParser()
        if not os.path.isfile('Config.ini'):
            f=codecs.open('Config.ini','w','gbk')
            f.write(u'''[default]
#需要刷的网址
URL=http://pncdzx2011.blog.163.com/

#每次刷的时间间隔，大于等于0
INTERVAL=0

#每次开启程序后刷的最大次数
MAX=200

#断网情况下重试的最大时间，单位为秒
TIMEOUT=300'''.replace('\n',os.linesep))
            f.close()
            return
        try:
            cf.read(self.CONF)
            self.URL=cf.get('default','URL')
            self.INTERVAL=cf.getint('default','INTERVAL')
            self.MAX=cf.getint('default','MAX')
            self.TIMEOUT=cf.getint('default','TIMEOUT')
        except Exception,e:
##            pass
            print str(e)
            print u'【warn】Config.ini is not complete, using default parameters for missing ones'

    def judgeNetwork(self, start=False):
        '''judge if network works'''
        urls = {
                "baidu"  : "http://www.baidu.com" ,
                "google" : "http://www.google.cn" ,
                "renren" : "http://www.renren.com",
                }
        isConnected = False
        begin=time.time()
        while isConnected == False:
            count=0
            for url in urls:
                try:
                    print u'检测网络中...\r',
                    hf = urllib2.urlopen(urls[url])
                    count += 1
                except Exception,e:
                    print u'请检查网络连接...'
                    break
            if count == len(urls.keys()):
                isConnected=True
            else:
                if time.time() - begin > self.TIMEOUT:
                    return False
                time.sleep(1)
        if start == True:
            try:
                urllib2.urlopen(self.URL)
                print u'start to BRUSH ' + self.URL
            except:
                print u'请检查Config.ini中的URL'
                return False
        
        return True
            
    def work(self):
        '''work work work...'''
        if not self.judgeNetwork(True):
            os.system('pause')
            return
        count=0
        failCount=0
        while count <= self.MAX:
            try:
                urllib2.urlopen(self.URL)
                count += 1
                print '  +' + str(count) + '\r',
                time.sleep(self.INTERVAL)
            except Exception,e:
                failCount += 1
                if failCount > 20:
                    if self.judgeNetwork():
                        continue
                    else:
                        os.system('pause')
                        return
        print 'Done!'
        os.system('pause')


if __name__=='__main__':
    Brusher().work()
        
